package com.mohe.shanpao.lucene;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class IndexerMain {

	public static void main(String[] args) throws IOException {
		File indexDir = new File("D:\\data\\indexLucene");
		File dataDir = new File("D:\\data\\dataLucene");
		
		long begin = System.currentTimeMillis();
		
		IndexWriter writer = getIndexWriter(indexDir.getPath(), true);
		
		index(writer,dataDir);
//		indexString(writer);
		
		writer.close();
		
		System.out.println("indexing field took "+(System.currentTimeMillis() - begin) + " ms");
	}
	
	
	private static void index(IndexWriter writer,File data) throws IOException
	{
		if(!data.exists() || !data.isDirectory())
		{
			throw new IOException(data + " does not exist or not directory");
		}
		
		indexDirectory(writer,data);
		
	}
	
	private static void indexDirectory(IndexWriter writer,File dataDir) throws IOException
	{
		File[] files = dataDir.listFiles();
		
		for(File file : files)
		{
			if(file.isDirectory())
			{
				indexDirectory(writer, file);
			}
			else if(file.getName().endsWith(".txt"))
			{
				indexFile(writer,file);
			}
		}
	}
	
	private static void indexFile(IndexWriter writer, File file) throws IOException
	{
		if(!file.exists() || file.isHidden() || !file.canRead())
		{
			return;
		}
		
		Document document = new Document();
		String content = FileUtils.readFileToString(file,Charset.forName("utf-8"));
		document.add(new TextField("content",content ,Field.Store.YES));
		document.add(new TextField("path",file.getCanonicalPath() ,Field.Store.YES));
		writer.addDocument(document);
	}
	
//	public static void indexString(IndexWriter writer) throws IOException
//	{
//		String descs[]={  
//	            "青岛是一个漂亮的城市。",  
//	            "南京是一个文化的城市。",  
//	            "上海是一个繁华的城市。" ,
//	            "油门和刹车用于欧洲杯"
//	    };
//		
//		for(int i=0;i<descs.length;i++){ 
//			Document doc=new Document();
//			doc.add(new TextField("desc", descs[i], Field.Store.YES));
//			
//			writer.addDocument(doc);  
//		}
//	}
	
	public static IndexWriter getIndexWriter(String indexPath,boolean create) throws IOException{
		Directory directory = FSDirectory.open(Paths.get(indexPath, new String[0]));
//		Analyzer analyzer = new StandardAnalyzer();
		SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(true);
		IndexWriterConfig config = new IndexWriterConfig(analyzer);
		if (create)
		{
			config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
		}
		else
		{
			config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
		}
		
		IndexWriter writer = new IndexWriter(directory, config);
		
		return writer;
		
	}
}
